package cn.lin.service.impl;

import cn.lin.domain.Quick;
import cn.lin.mapper.QuickMapper;
import cn.lin.service.QuickService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author zxy
 * @date 2024/12/6 18:40
 */
@Service
public class QuickServiceImpl implements QuickService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate ;

    @Autowired
    private RabbitTemplate rabbitTemplate ;

    @Autowired
    private QuickMapper quickMapper ;

    @Override
    public Quick quick(Quick quick) {
        synchronized (QuickServiceImpl.class){
            Long res = redisTemplate.opsForValue().decrement("quick:"+quick.getId(),1);
            if (res < 0){
                //抢答失败
                quick.setIsQuick("未抢到");
                return quick;
            }else {
                //抢答成功 发送消息到mq
                rabbitTemplate.convertAndSend("quick.queue",quick);
                System.out.println("-------------"+quick.getQuickUsername());
                quick.setIsQuick("抢到了") ;
                return quick ;
            }
        }
    }

    @Override
    public int addQuick(Quick quick) {
        return quickMapper.addQuick(quick);
    }

    @Override
    public Quick findID(String quickName) {
        return quickMapper.findID(quickName);
    }
}
